package com.ww.springboot.boot.test;

import cn.hutool.http.HttpRequest;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageDimensions {
    public static void main(String[] args) {
        try {
            byte[] bytes = HttpRequest
                    .get("https://img.cdnfe.com/product/fancy/0d7e1fd6-4428-413b-9670-1e0dede9462c.jpg").execute().bodyBytes();
            // 读取图片文件
            File tempFile = null;
            try {
                tempFile = File.createTempFile("example-", ".txt"); // 创建临时文件
                // 使用 FileOutputStream 将 byte[] 写入文件
                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                    fos.write(bytes);
                }

                // 输出文件路径
                System.out.println("File created: " + tempFile.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            BufferedImage bufferedImage = ImageIO.read(tempFile);

            // 获取图片的宽度和高度
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            // 打印图片的宽度和高度
            System.out.println("Width: " + width);
            System.out.println("Height: " + height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
